/*
 * Created on 11.09.2006 by leck
 *
 */
package de.devisnik.eidle.picture;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

public class ImageUtils {

	private ImageUtils() {
	}

	private static Rectangle getTargetRectangle(Rectangle imageRectangle,
			Rectangle displayRectangle) {
		double widthProportion = getProportion(displayRectangle.width,
				imageRectangle.width);
		double heightProportion = getProportion(displayRectangle.height,
				imageRectangle.height);
		double minimalProportion = Math.min(widthProportion, heightProportion);
		int resultWidth = (int) (imageRectangle.width * minimalProportion);
		int resultHeight = (int) (imageRectangle.height * minimalProportion);
		int resultX = (displayRectangle.width - resultWidth) / 2;
		int resultY = (displayRectangle.height - resultHeight) / 2;
		return new Rectangle(resultX, resultY, resultWidth, resultHeight);
	}

	public static void drawImage(GC gc, Image image, Rectangle displayRectangle) {
		Rectangle imageRectangle = image.getBounds();
		Rectangle targetRectangle = getTargetRectangle(imageRectangle,
				displayRectangle);
		gc.fillRectangle(0, 0, targetRectangle.x, displayRectangle.height);
		gc.fillRectangle(0, 0, displayRectangle.width, targetRectangle.y);
		gc.fillRectangle(0, targetRectangle.y + targetRectangle.height,
				displayRectangle.width, displayRectangle.height
						- (targetRectangle.y + targetRectangle.height));
		gc.fillRectangle(targetRectangle.x + targetRectangle.width, 0,
				displayRectangle.width
						- (targetRectangle.x + targetRectangle.width),
				displayRectangle.height);
		gc.setAntialias(SWT.ON);
		gc.drawImage(image, 0, 0, imageRectangle.width, imageRectangle.height,
				targetRectangle.x, targetRectangle.y, targetRectangle.width,
				targetRectangle.height);
	}

	private static double getProportion(int x, int y) {
		return ((double) x) / y;
	}

	public static Image getScaledImage(Display display, Image image,
			Rectangle displayRectangle) {
		ImageData imageData = new ImageData(displayRectangle.width,
				displayRectangle.height, 8, new PaletteData(0xff0000, 0x00ff00,
						0x0000ff));
		Image scaledImage = new Image(display, imageData);
		GC gc = new GC(scaledImage);
		ImageUtils.drawImage(gc, image, displayRectangle);
		gc.dispose();
		return scaledImage;
	}

	public static Image getInnerImage(Display display, Image image,
			int borderX, int borderY) {
		Rectangle imageBounds = image.getBounds();
		int innerWidth = imageBounds.width - borderX;
		int innerHeight = imageBounds.height - borderY;
		Image scaledImage = new Image(display, new Rectangle(0, 0, innerWidth,
				innerHeight));
		GC gc = new GC(scaledImage);
		gc.drawImage(image, borderX / 2, borderY / 2, innerWidth, innerHeight,
				0, 0, innerWidth, innerHeight);
		gc.dispose();
		return scaledImage;
	}

	public static int ggt(int a, int b) {
		if (a == b) {
			return a;
		}
		int max = Math.max(a, b);
		int min = Math.min(a, b);
		return ggt(max - min, min);
	}

}
